import java.io.*;
import java.util.Date;

/**
 * So I'm using blocking IO.....
 * 
 * And this is how. So I have this specifically threaded reader 
 * that just sits and spins until input comes down the line 
 * (which incidentally is a lot of the time). Then, when input
 * comes, it fires it back up the line to pir_io 
 * 
 * @author Sam Reese
 *
 */
public class pir_io_reader extends Thread {
	private InputStream io;
	private pir_io parent;
	private long[] statuses;
//	private Date myDate;
	
	/**
	 * The constructor that works!
	 * 
	 * @param parent_ref backreference to pir_io
	 * @param iioo the input stream, hopefully serial!
	 */
	public pir_io_reader(pir_io parent_ref, InputStream io_param){
		parent = parent_ref;
		io = io_param;
		statuses = new long[(parent.effects + 1)]; 
		for (int i = 0; i <= (parent.effects); i++){
			statuses[i] = 0;
		}
		//myDate = new Date();
	}
	
	/**
	 * Sit and Spin! It just blocks on IO forever, and passes up 
	 * what it gets. That easy. 
	 * 
	 * Actually not. So it gets input on the sensor panel by polling
	 * each sensor address (parent.PIR_SENSORS_ADDRESSES), then 
	 * checks that value against PIR_SENSOR_HIGH_START, which is the
	 * bound for the sensor information. If that value is in the right
	 * range, it increments the statuses value corresponding to that 
	 * sensor. 
	 * 
	 * When the statuses[i] is greater or equal to the PIR_SENSOR_THRESH
	 * value, it turns on the corresponding relay. 
	 * 
	 * When a value les than the pir_sensor_high_start value is
	 * recieved, it either decrements or leaves at 0 the statuses
	 * array.
	 * 
	 * This is to allow for PIR_SENSOR_THRESH individual bad readings
	 * to come down the sensorline. 
	 * 
	 * 
	 * @see pir_io
	 */
	public void run(){
		 
            int c = 0;
            while(true) {
            	try{
            		for (int i = 1; i <= parent.effects; i++){
            			parent.sendToPort(parent.PIR_SENSOR_ADDRESSES[i]);
            			c = io.read();
            			if(parent.DEBUG_SERIAL)
            				System.out.println("SERIAL: Sensor " + i + " gave " + c + "on status " + statuses[i]);
            			if ( c > parent.PIR_SENSOR_HIGH_START ){
            				if (statuses[i] <= parent.PIR_SENSOR_THRESH){
            					statuses[i]++;
            				}
            				if (statuses[i] > parent.PIR_SENSOR_THRESH){
            					parent.fire(i, parent.PIR_HIGH_ON);
            				}
            	
            			}
            			if (c <= parent.PIR_SENSOR_HIGH_START){
            				if (statuses[i] <= 0){
            					//perhaps turn off to be safe?
            					continue;
            				}else{
            					statuses[i]--;
            					if (statuses[i] < parent.PIR_SENSOR_THRESH){
            						parent.fire(i, parent.PIR_HIGH_OFF);
            					}
     	      				}
            				
            			}
            		}
             		try{	 
             		 // the interval to wait between pollings
             		Thread.sleep(500);
            		} catch (InterruptedException e){
            			System.out.println("interrupted"  + e);
            		}
            		
            	} catch (IOException ie){
            		if (pir_io.DEBUG_SERIAL)
            			System.out.println("IOEexception from reader " + ie);
            	}

     
       

		
            	
            } 
	}
            
}

